package com.computergames.server.domain;

import java.util.*;

public class Deck<T extends Enum<T>> {
    private List<T> cards;
    private int numberOfDecks;

    public Deck(int numberOfDecks, Class<T> cardClass) {
        this.numberOfDecks = numberOfDecks;
        this.cards = new ArrayList<>();
        generateDecks(cardClass);
    }

    /**
     * 根据游戏类型生成多副牌
     */
    private void generateDecks(Class<T> cardClass) {
        for (int i = 0; i < numberOfDecks; i++) {
            for (T card : cardClass.getEnumConstants()) {
                cards.add(card);
            }
        }
        Collections.shuffle(cards); // 洗牌
    }

    /**
     * 发牌
     * @return 最上面的一张牌
     */
    public T drawCard() {
        if (cards.isEmpty()) {
            throw new IllegalStateException("No more cards in the deck.");
        }
        return cards.removeFirst();
    }

    /**
     * 获取剩余牌的数量
     * @return 剩余牌的数量
     */
    public int remainingCards() {
        return cards.size();
    }

    /**
     * 洗牌
     */
    public void shuffle() {
        Collections.shuffle(cards);
    }
}
